{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "50177bd6",
   "metadata": {},
   "source": [
    "#### 上周回顾\n",
    "> 1.函数的定义：def\n",
    "> 2.函数名的定义规则-->变量名一样的规则\n",
    "> 3.调用函数的方式\n",
    "> 4.参数\n",
    ">> 1.形式参数（定义函数）\n",
    ">> 2.实际参数（调用函数）\n",
    ">> 3.有多个参数时：\n",
    ">>> 0. 关键字参数\n",
    ">>> 1. 位置参数（调用函数时）-->关键字参数要均放在末尾（多个不考虑顺序）\n",
    ">>> 2. 默认参数（定义函数时）-->默认参数均放在末尾（多个不考虑顺序）\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8097760",
   "metadata": {},
   "source": [
    "### 本周知识点\n",
    "> 1. 函数的返回值\n",
    "> 2. 函数的参数类型\n",
    "> 3. 模块与函数的关系\n",
    "> 4. 定义一个模块，然后调用模块\n",
    ">> 1. 调用模块\n",
    ">> 2. 调用模块中的函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23e5abdb",
   "metadata": {},
   "source": [
    "#### 1.函数的返回值\n",
    "\n",
    "> 1. 返回值的关键字：return\n",
    "> 2. 在其他的编程语言中，一般return一定要写，不写会报错；但是在python中，return可以不写，return 默认为 None\n",
    ">>  1.编程语言中的函数，一定有返回值\n",
    ">>> 2.返回值的结果是一个值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "47e228cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "    # 函数的返回结果为 full_name\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "10d59a47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Li Hua\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"Li\",\"Hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9bf99596",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Li Hua\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "musician = get_formatted_name(\"Li\",\"Hua\")\n",
    "print(musician)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f22b3e1",
   "metadata": {},
   "source": [
    "### 函数的参数\n",
    "\n",
    "#### 1. 函数输入的参数\n",
    "> 1. 函数可以输入复杂的参数\n",
    ">> 1.函数参数为列表\n",
    ">> 2.函数参数为字典\n",
    "> 2. 传递任意数量的实参\n",
    ">> A.*args\n",
    ">> B.*kwargs\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fb7a3dc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \"\"\"函数输入一个列表\"\"\"\n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ce415958",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Lihua', 'Ziyin', 'Juicy']\n"
     ]
    }
   ],
   "source": [
    "list_test([\"Lihua\",\"Ziyin\",\"Juicy\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7710a5a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict_test(request_header):\n",
    "    \"\"\"函数输入一个字典\"\"\"\n",
    "    print(request_header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1d9fe85f",
   "metadata": {},
   "outputs": [],
   "source": [
    "httpbin_get = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61ad73c8-2257df6f0fcd7e27730c6cee\"\n",
    "  }, \n",
    "  \"origin\": \"223.104.66.140\", \n",
    "  \"url\": \"http://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "8b721a15",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Host': 'httpbin.org', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36', 'X-Amzn-Trace-Id': 'Root=1-61ad73c8-2257df6f0fcd7e27730c6cee'}\n"
     ]
    }
   ],
   "source": [
    "dict_test(httpbin_get[\"headers\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64a771f7",
   "metadata": {},
   "source": [
    "> 2.传递任意数量的实参\n",
    ">> 1. *args\n",
    ">>> 1. args 可以接受多个数据\n",
    ">>> 2. args 将多个数据存在一个元组当中，实际还是一个变量\n",
    ">> 2. ** kwargs\n",
    ">>> 1. kwargs 可以接受多个数据（键值对）==> 关键字参数\n",
    ">>> 2. kwargs 将多个 键值对 的数据存放在一个字典中，实际上还是一个变量\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "46af1d32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('mushroom', 'green peppers', 'extra cheese')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 回顾元组的定义\n",
    "toppings = \"mushroom\",\"green peppers\",\"extra cheese\"\n",
    "toppings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2e33a35c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# *args 实践\n",
    "def make_food(food,*toppings):\n",
    "    \"\"\"打印顾客点的所有配料\"\"\"\n",
    "    return food,toppings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9948ab9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "build_food = make_food(\"pizza\",\"mushroom\",\"green peppers\",\"extra cheese\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "cf6ac2d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers', 'extra cheese'))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "build_food"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "29610610",
   "metadata": {},
   "outputs": [],
   "source": [
    "# *kwargs 实践\n",
    "def person(name,**info):\n",
    "    \"\"\"打印个人的信息\"\"\"\n",
    "    return name,info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "09ce9e5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "build_person = person(\"Lihua\",age=18,job=\"student\",address=\"广州南方学院\",e_mail=\"123456@qq.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f409b26b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Lihua',\n",
       " {'age': 18, 'job': 'student', 'address': '广州南方学院', 'e_mail': '123456@qq.com'})"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "build_person"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2e2b86c",
   "metadata": {},
   "source": [
    "### 2.函数返回的结果\n",
    "\n",
    "> 1. 函数可以返回任意类型的值，包括列表、字典和集合等较为复杂的数据结构\n",
    ">> 1.函数返回一个集合\n",
    ">> 2.函数返回一个字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5cc05410",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回值为一个集合\n",
    "def search4vowels(word):\n",
    "    \"\"\"Return a boolean based on any vowels found\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    return found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "faaff07e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'e', 'a', 'o', 'i'}\n"
     ]
    }
   ],
   "source": [
    "word = search4vowels('hello Zhichao')\n",
    "print(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "7d701f4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回值为一个字典\n",
    "def build_person(name,age,job):\n",
    "    \"\"\"返回一个字典，其中包含有关一个人的信息\"\"\"\n",
    "    person = {'name':name,'age':age,'job':job}\n",
    "    return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e5c0156b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Lihua', 'age': 18, 'job': 'student'}\n"
     ]
    }
   ],
   "source": [
    "person = build_person(\"Lihua\",18,\"student\")\n",
    "print(person)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5db867b9",
   "metadata": {},
   "source": [
    "### 3. 模块\n",
    "\n",
    "> 1. 模块 用户都不需要看底层代码\n",
    "> 2. 调用模块的方法\n",
    ">> 1. 必须先调用模块，才可以使用\n",
    ">>> 1. import moudle_name 直接调用整个模块，此时模块的所有函数处于备用状态，使用方法为：\n",
    "mould_name.func_name()\n",
    ">>> 2. from moudle_name import func_name,此时仅调用模块的某一个函数，只能使用这个模块的该函数，使用方法为func_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "a517ab2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import study\n",
    "study.search4vowels('hello zhichao')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4afcc8ff",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name 'news_链接' from 'study' (E:\\Python project_Xu\\week_15\\study.py)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-34-3383ba2e52cb>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mstudy\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnews_链接\u001b[0m \u001b[1;31m# 该方法不需要再写模块的名字+.func_name()\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m: cannot import name 'news_链接' from 'study' (E:\\Python project_Xu\\week_15\\study.py)"
     ]
    }
   ],
   "source": [
    "from study import news_链接  # 该方法不需要再写模块的名字+.func_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "773b88c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "news_链接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "646e9b71",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：抓取某个校园新闻的所有文本链接--- 广州南方学院为例\n",
    "\n",
    "url = \"https://www.nfu.edu.cn/\"\n",
    "def news_链接(url,新闻分类):\n",
    "    \"\"\"抓取学校新闻的所有链接\"\"\"\n",
    "    for i in page:\n",
    "        if i == 1:\n",
    "            index_链接 = Url+ 新闻分类 +‘/index.html’\n",
    "            news_链接.append(index_链接)\n",
    "        else:\n",
    "            index_链接 = url + 新闻分类 + ‘/index’+ i+'.html'\n",
    "            "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
